package com.jinlejia.erp.ui.common;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityLoginApplyBinding;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class LoginApplyActivity extends AppCompatActivity {
    private ActivityLoginApplyBinding binding;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login_apply);
        binding.setEvent(this);
        progressDialog = new ProgressDialog(this, "请稍后...", false);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;

            case R.id.tvLoginApply:
                postLoginApply();
                break;
        }
    }

    /****
     * 请求申请登录接口
     */
    private void postLoginApply() {
        progressDialog.show();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pin", getIntent().getStringExtra("pin"));
        jsonObject.put("password", getIntent().getStringExtra("password"));
        jsonObject.put("deviceCode", getIntent().getStringExtra("deviceCode"));
        jsonObject.put("phoneModel", Build.MODEL);

        OkHttpUtils
                .postString()
                .url(ApiConfig.APPLY_LOGIN)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                            Toast.makeText(LoginApplyActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        if (Integer.parseInt(response.get("code").toString()) == 200) {
                            //TODO:申请成功
                            DoubleCommonDialog doubleCommonDialog = new DoubleCommonDialog(LoginApplyActivity.this,String.valueOf(response.get("message")),false);
                            doubleCommonDialog.show();
                            doubleCommonDialog.setConfirmListener(() -> finish());
                        } else if (Integer.parseInt(response.get("code").toString()) == 123) {
                            //TODO:重复申请
                            DoubleCommonDialog doubleCommonDialog = new DoubleCommonDialog(LoginApplyActivity.this,String.valueOf(response.get("message")),false);
                            doubleCommonDialog.show();
                            doubleCommonDialog.setConfirmListener(() -> finish());
                        }else if (Integer.parseInt(response.get("code").toString()) == 400){
                            CustomToast.showToast(LoginApplyActivity.this,"系统错误",1500);
                        }
                    }
                });


    }


}
